package com.niit.dao.impl;

import java.util.List;

import com.niit.bean.News;
import com.niit.dao.NewsDao;
import com.niit.utils.DbUtil;

public class NewsDaoImpl implements NewsDao {

	private DbUtil util =new DbUtil();
	@Override
	public int add(News t) {
		String sql = "insert into tb_news (kind,title,content) values (?,?,?)";
		int a = util.update(sql, t.getKind(),t.getTitle(),t.getContent());
		return a;
	}

	@Override
	public int delete(News t) {
		String sql="delete from tb_news where id=? ";
		int a=util.update(sql, t.getId());
		return a;
	}

	@Override
	public int update(News t) {
		String sql="update tb_news set kind=?,title=?,content=?,time=? ";
		int a=util.update(sql, t.getKind(),t.getTitle(),t.getContent(),t.getTime());
		return a;
	}

	@Override
	public List<News> findAll(int start, int end) {
		String sql="select * from(select rownum r,tb_news.* from tb_news) a where a.r>? and a.r<=?";
		List<News> list =util.select(sql, News.class, start,end);
		return list;
	}

	@Override
	public int findTotal() {
		String sql = "select count(*) from tb_news ";
		int count = util.selectCount(sql);
		return count;
	}

	@Override
	public List<News> findByKind(String kind, int start, int end) {
		String sql="select * from(select rownum r,tb_news.* from tb_news where kind = ?) a where a.r>? and a.r<=?";
		List<News> list =util.select(sql, News.class, kind, start, end);
		return list;
	}

	@Override
	public News findById(String id) {
		String sql = "select * from tb_news where id = ?";
		List<News> list = util.select(sql, News.class, id);
		int count = list.size();
		if(count == 0){
			return null;
		}else{
			News news = list.get(0);
			return news;
		}
	}

}
